﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Xml.Serialization;
using MatchResults.Helpers;
using MatchResults.Model;
using Microsoft.Phone.Info;

namespace MatchResults
{
    public partial class ResultShow : PhoneApplicationPage
    {

        #region [ Page Load ]
        
        public ResultShow()
        {
            InitializeComponent();
            // 
            
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            string matchType = string.Empty;
            string matchTypeText = string.Empty;

            //if (this.NavigationContext.QueryString.TryGetValue("MatchType", out matchType))
            //{
            //    FutbolMerkeziService.MatchType CurrentType = (FutbolMerkeziService.MatchType)Enum.Parse(typeof(FutbolMerkeziService.MatchType), matchType, true);
            //    GetStandings(CurrentType);
            //    GetMatchesList(CurrentType);
            //    GetTopScorers(CurrentType);
            //}
            //if (this.NavigationContext.QueryString.TryGetValue("text", out matchTypeText))
            //{
            //    pivotPage.Title = matchTypeText;

            //}
        }

        
        #endregion

        //#region [ Top Scores ]

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="MatchType"></param>
        //private void GetTopScorers(FutbolMerkeziService.MatchType MatchType)
        //{
        //    FutbolMerkeziService.FM_Web_ServiceSoapClient FM_Web_Service = new FutbolMerkeziService.FM_Web_ServiceSoapClient();
        //    FM_Web_Service.GetTopScorersCompleted += new EventHandler<FutbolMerkeziService.GetTopScorersCompletedEventArgs>(FM_Web_Service_GetTopScorersCompleted);
        //    FM_Web_Service.GetTopScorersAsync(MatchType);
        //}

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //void FM_Web_Service_GetTopScorersCompleted(object sender, FutbolMerkeziService.GetTopScorersCompletedEventArgs e)
        //{
        //    if (e.Result.Any1.Nodes().Count() == 0)
        //    {
        //        return;
        //    }
        //    byte[] buffer = System.Text.Encoding.Unicode.GetBytes(e.Result.Any1.Nodes().First().ToString());
        //    MemoryStream ms = new MemoryStream(buffer);

        //    ms.Position = 0;

        //    // deserialization
        //    var topScorers = (NewDataSet)XMLSerializerHelper.Deserialize(ms, typeof(NewDataSet));

        //    ms.Close();
        //    int i = 0;
        //    foreach (var item in topScorers.Table)
        //    {
        //        item.RowNo = ++i;
        //    }
        //    grdTopScorers.ItemsSource = topScorers.Table;//.Select(c => new { Id = i++, NAME_TR = c.NAME_TR, GOALCOUNT = c.GOALCOUNT }).ToList();
        //}

        //#endregion

        //#region [ MatchList ]

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="MatchType"></param>
        //private void GetMatchesList(FutbolMerkeziService.MatchType MatchType)
        //{
        //    FutbolMerkeziService.FM_Web_ServiceSoapClient FM_Web_Service = new FutbolMerkeziService.FM_Web_ServiceSoapClient();
        //    FM_Web_Service.GetMatchesListCompleted += new EventHandler<FutbolMerkeziService.GetMatchesListCompletedEventArgs>(FM_Web_Service_GetMatchesListCompleted);
        //    FM_Web_Service.GetMatchesListAsync(MatchType);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //void FM_Web_Service_GetMatchesListCompleted(object sender, FutbolMerkeziService.GetMatchesListCompletedEventArgs e)
        //{
        //    if (e.Result.Any1.Nodes().Count() == 0)
        //    {
        //        return;
        //    }
        //    byte[] buffer = System.Text.Encoding.Unicode.GetBytes(e.Result.Any1.Nodes().First().ToString());
        //    MemoryStream ms = new MemoryStream(buffer);

        //    ms.Position = 0;

        //    // deserialization
        //    var matches = (NewDataSet)XMLSerializerHelper.Deserialize(ms, typeof(NewDataSet));

        //    ms.Close();
        //    grdMatchList.ItemsSource = matches.Table;
        //}

        //#endregion

        //#region [ Standings ]

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="MatchType"></param>
        //public void GetStandings(FutbolMerkeziService.MatchType MatchType)
        //{
        //    FutbolMerkeziService.FM_Web_ServiceSoapClient FM_Web_Service = new FutbolMerkeziService.FM_Web_ServiceSoapClient();
        //    FM_Web_Service.GetStandingsCompleted += new EventHandler<FutbolMerkeziService.GetStandingsCompletedEventArgs>(FM_Web_Service_GetStandingsCompleted);
        //    FM_Web_Service.GetStandingsAsync(MatchType);
        //}

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //void FM_Web_Service_GetStandingsCompleted(object sender, FutbolMerkeziService.GetStandingsCompletedEventArgs e)
        //{
        //    if (e.Result.Any1.Nodes().Count() == 0)
        //    {
        //        return;
        //    }
        //    byte[] buffer = System.Text.Encoding.Unicode.GetBytes(e.Result.Any1.Nodes().First().ToString());
        //    MemoryStream ms = new MemoryStream(buffer);

        //    ms.Position = 0;

        //    // deserialization
        //    var Standings = (NewDataSet)XMLSerializerHelper.Deserialize(ms, typeof(NewDataSet));

        //    ms.Close();
          
        //    grdStandingTable.ItemsSource = Standings.Table;
        //}

        //#endregion
    }
}